﻿using MCM.Abstractions.Base.Global;
using MarriageFertility.Texts;
using TaleWorlds.Localization;

namespace MarriageFertility.Mcm
{
    public class MCMSettings
    {
        public int MinimumMarriageAgeMale;
        public int MinimumMarriageAgeFeMale;
        public bool EnableMoreSpouse;
        public bool EnableSameGender;
        public bool EnableRelatedMarriage;

        public bool EnablePrisonMarryDialog;
        public bool EnableSexPregnancyDialog;
        public bool EnableSexFriendDialog;
        public bool EnableRentWifeDialog;
        public bool EnableDuelSpouseDialog;
        public bool EnablePoemDialog;
        public bool EnableCompetitionQuest;

        public bool EnableNpcMarriageChance;
        public float BaseMarriageChanceForNPCs;
        public bool OnlyPlayerMarryMore;
        public bool IgnoreKingdom;
        public bool IgnoreClan;

        public int MinPregnancyAge;
        public int MaxPregnancyAge;
        public float PregnancyDurationInDays;
        public float MaternalMortalityProbabilityInLabor;
        public float StillbirthProbability;
        public float DeliveringFemaleOffspringProbability;
        public float DeliveringTwinsProbability;

        public bool EnablePregnancyModify;
        public float MainHeroProbability;
        public float MainHeroClanProbability;
        public float OtherHeroProbability;
        public int PlayerMaxChildrenLimit;
        public int LordMaxChildrenLimit;

        public bool PrisonPregnancyEnable;
        public bool PrisonOnlyPlayerParty;
        public float PrisonPregnancyProbability;

        public bool JoinPlayerClan;
        public bool ChildrenBecomeHuman;
        public bool ChildJoinMotherClan;
        public int ChildrenBecomeHumanIncreaseAge;

        public bool RelationCheat;
        public bool PersonRelation;
        public bool FixClanLeader;
        public bool KnowEveryOne;
        public int HeroOldAge;
        public bool EnableNameShow;
        public bool LordAutoTrade;
        public bool EnableClearSpouse;
        
        public int DaysInTownThreshold;
        public int RequestTimeoutDays;
        public float EliteTroopRatio;
        public bool EnablePartyLeave;

        private static MCMSettings _instance;
        private FluentGlobalSettings _globalSettings;
        public bool FirstRunDone;

        public static MCMSettings Instance => _instance ??= new MCMSettings();

        public void Settings()
        {
            TextObject id = new TextObject("MarriageAndPregnancy");
            string modName = CommonText.ModName;
            string version = "0.2.10";
            var builder = SettingsMenu.CreateBaseSettingsBuilder(id.ToString(), $"{modName} {version}");
            SettingsMenu.AddMarriageSettings(builder);
            SettingsMenu.AddNPcMarriageSettings(builder);
            SettingsMenu.AddPregnancyBaseSettings(builder);
            SettingsMenu.AddPregnancyProbabilitySettings(builder);
            SettingsMenu.AddPrionMarriageFertility(builder);
            SettingsMenu.AddDialogSettings(builder);
            SettingsMenu.AddChildrenSettings(builder);
            SettingsMenu.AddPartyLeaveSettings(builder); // New settings group
            SettingsMenu.AddDefaultSetting(builder);
            _globalSettings = builder.BuildAsGlobal();
            _globalSettings.Register();

            if (!FirstRunDone)
                FirstSetUp();
        }

        private void FirstSetUp()
        {
            FirstRunDone = true;
            MinPregnancyAge = 18;
            MaxPregnancyAge = 45;
            PregnancyDurationInDays = 36;
            MaternalMortalityProbabilityInLabor = 1.5f;
            StillbirthProbability = 1f;
            DeliveringFemaleOffspringProbability = 51f;
            DeliveringTwinsProbability = 3f;

            EnablePrisonMarryDialog = true;
            EnableSexFriendDialog = true;
            EnableSexPregnancyDialog = true;
            EnablePoemDialog = true;
            EnableRentWifeDialog = true;
            EnableDuelSpouseDialog = true;
            EnableCompetitionQuest = true;

            BaseMarriageChanceForNPCs = 0.2f;
            OnlyPlayerMarryMore = false;
            IgnoreKingdom = false;
            IgnoreClan = false;
            MinimumMarriageAgeMale = 18;
            MinimumMarriageAgeFeMale = 18;
            EnableMoreSpouse = false;
            EnableSameGender = false;
            EnableRelatedMarriage = false;
            ChildJoinMotherClan = false;
            JoinPlayerClan = false;

            EnablePregnancyModify = false;
            MainHeroProbability = 2f;
            MainHeroClanProbability = 2f;
            OtherHeroProbability = 2f;
            PlayerMaxChildrenLimit = 30;
            LordMaxChildrenLimit = 30;

            PrisonPregnancyEnable = true;
            PrisonOnlyPlayerParty = false;
            PrisonPregnancyProbability = 2f;

            ChildrenBecomeHuman = false;
            ChildrenBecomeHumanIncreaseAge = 30;

            EnableNameShow = true;

            RelationCheat = false;
            PersonRelation = true;
            FixClanLeader = true;
            KnowEveryOne = false;
            LordAutoTrade = false;
            EnableClearSpouse = true;
            HeroOldAge = 47;
            
            DaysInTownThreshold = 1;
            RequestTimeoutDays = 2;
            EliteTroopRatio = 50f;
            EnablePartyLeave = true;
        }
    }
}